<?php
/*
    功能: 图片上传

        Array(
            [name] => Array
                (
                    [0] => chinayu.png
                )

            [type] => Array
                (
                    [0] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => C:\Windows\phpBFC3.tmp
                )

            [error] => Array
                (
                    [0] => 0
                )

            [size] => Array
                (
                    [0] => 100595
                )

        )

        |
        V
        array(
            array(
                'name'=>'',
                'tmp_name'=>''
            ),
            array(
                'name'=>'',
                'tmp_name'=>''
            )
        )

*/


if(!empty($_FILES['imgs']['name'][0])){
    // echo '<pre>';
    //对$_FILES数组进行处理
    foreach($_FILES['imgs'] as $key=>$val){
       foreach($val as $k=>$v){
            $imgs[$k][$key]=$v;
       }
    }
    $date=date('Y-m-d',time());
    $path_name='uploads/';
   
    // print_r($imgs);
    $img_list=[];
    foreach($imgs as $key=>$val){
        $file_name=time().'-'.rand(1000,9999).strrchr($val['name'],'.');
        $full_name=$path_name.$file_name;
        $ret=move_uploaded_file($val['tmp_name'],$full_name);
        if($ret){
            $img_list[]=$full_name;
            $tips_success='上传成功';
        }else{
            $tips_error='上传失败';
        }
    }
    
}else{
    $tips_notice="请选择图片";       
}
//  echo '</pre>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片上传预览-day03</title>
    <link rel="stylesheet" href="static/css/bootstrap.min.css">
    <link rel="stylesheet" href="static/css/style.css">
    <script src="static/js/jquery.min.js"></script>
    <script src="static/js/bootstrap.min.js"></script>
</head>
<body>

<?php

//引入公共头部
require_once 'header.php';

?>
<div class="container well main my-well">
    
    <!-- <h1 class="text-center">图片上传预览</h1>
    <hr/> -->

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    
    <div class="form-group">
            <?php
                if (!empty($tips_success)):
            ?>
             <div class="alert alert-success alert-dismissible" role='alert'>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <b>
                <?php
                    echo $tips_success;
                ?>    
                </b>        
               
            </div>
            <?php
            endif;
            ?>
            <?php
                if (!empty($tips_error)):
            ?>
             <div class="alert alert-danger alert-dismissible" role='alert'>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <b>
                <?php
                    echo $tips_error;
                ?>
                </b>
            </div>
            <?php
                endif;
            ?>
            <?php
                if (!empty($tips_notice)):
            ?>
            <div class="alert alert-warning alert-dismissible" role='alert'>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <b>
                <?php
                    echo $tips_notice;
                ?>
                </b>
            </div>
            <?php
                endif;
            ?>
    </div>

    <div class="form-group upload-group">
        <div id="btn-file-select">请选择文件</div>
        <input  id="input-file" type="file" name="imgs[]" multiple accept='.png,.jpg,.gif'>
    </div>

    <div class='form-group'>
        <button class="btn btn-primary btn-lg btn-block">立即上传</button>
    </div>

   
    </form>

    <div class="row">
        <?php
            if(!empty($img_list)):
                foreach($img_list as $val):
        ?>
        <div class="col-sm-6 col-md-4">
            <div class="thumbnail">
                <img src="<?php echo $val;?>" alt="...">
            </div>
        </div>
        <?php
                endforeach;
            endif;
        ?>
    </div>           

</div>


<?php

//引入公共底部
require_once 'footer.php';

?>

<script>
var btn=document.getElementById('btn-file-select');
var inputFile=document.getElementById('input-file');
btn.onclick=function(){
    inputFile.click();
}
</script>
</body>
</html>